{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "43283e39",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T06:34:01.119523200Z",
     "start_time": "2025-09-10T06:34:01.097522Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn.functional as F\n",
    "from torch.utils.data import Dataset, DataLoader\n",
    "import copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "20d691cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 0.4416,  0.3734, -0.5215, -0.5165, -0.7766, -1.6770,  1.1801,  1.0239,\n",
       "        -2.0274,  0.0531, -0.3702, -0.4232,  1.0565,  0.6814,  0.0350,  0.6340,\n",
       "         0.5915,  0.7168, -0.2896,  0.3005, -0.3082,  0.0102, -0.7529, -0.9317,\n",
       "         0.1758])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.randn(5,5).reshape(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fff5147d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[-1.3700, -0.0765],\n",
       "         [-2.2818,  0.4752],\n",
       "         [-0.4068,  1.3529]],\n",
       "\n",
       "        [[ 1.0273,  0.1747],\n",
       "         [ 0.0859,  0.5419],\n",
       "         [ 0.3126, -0.4098]]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=torch.randn(2,3,2)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2b9fcf86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1.3700, -0.0765],\n",
       "        [-2.2818,  0.4752],\n",
       "        [-0.4068,  1.3529],\n",
       "        [ 1.0273,  0.1747],\n",
       "        [ 0.0859,  0.5419],\n",
       "        [ 0.3126, -0.4098]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(-1, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "456dbc14",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T06:33:58.698847400Z",
     "start_time": "2025-09-10T06:33:58.680847100Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ True, False, False],\n",
       "        [ True,  True, False],\n",
       "        [ True,  True,  True]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.triu(torch.ones(3, 3), diagonal=1) == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9590428",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([7, 4, 5, 3, 2])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.randint(1,9,size=(5,))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d83077c7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[8, 6, 3, 3, 5],\n",
      "        [3, 6, 2, 7, 4],\n",
      "        [6, 1, 7, 8, 3],\n",
      "        [1, 6, 5, 7, 1],\n",
      "        [4, 5, 8, 2, 5]]) tensor([[0, 8, 6, 3, 3, 5, 9],\n",
      "        [0, 3, 6, 2, 7, 4, 9],\n",
      "        [0, 6, 1, 7, 8, 3, 9],\n",
      "        [0, 1, 6, 5, 7, 1, 9],\n",
      "        [0, 4, 5, 8, 2, 5, 9]])\n"
     ]
    }
   ],
   "source": [
    "class SimpleDataset(Dataset):\n",
    "    def __init__(self):\n",
    "        prefix = torch.tensor([0])  # 前面插入0\n",
    "        suffix = torch.tensor([9])  # 后面追加9\n",
    "\n",
    "        self.data = [torch.randint(1,9,size=(5,)) for _ in range(100)]\n",
    "        # self.labels = [torch.randint(0,10,size=(5,)) for _ in range(10)]\n",
    "        self.labels = [torch.cat([prefix,_,suffix],dim=0) for _ in copy.deepcopy(self.data)]\n",
    "\n",
    "    def __len__(self):\n",
    "        return len(self.data)\n",
    "\n",
    "    def __getitem__(self, idx):\n",
    "        return self.data[idx], self.labels[idx]\n",
    "\n",
    "def create_data():\n",
    "    dataset = SimpleDataset()\n",
    "    data_loader = DataLoader(dataset, batch_size=5, shuffle=True)\n",
    "    # for _ in data_loader:\n",
    "    #     print(_[0])\n",
    "    #     print(_[1])\n",
    "    #     print('\\n')\n",
    "    return data_loader\n",
    "\n",
    "for a,b in create_data():\n",
    "    print(a,b)\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7d2fc8f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1, 6, 7, 6, 1],\n",
      "        [1, 8, 7, 8, 2],\n",
      "        [8, 2, 3, 2, 5],\n",
      "        [1, 6, 5, 2, 5],\n",
      "        [2, 1, 7, 3, 2]])\n"
     ]
    }
   ],
   "source": [
    "class SimpleDataset(Dataset):\n",
    "    def __init__(self):\n",
    "        prefix = torch.tensor([0])  # 前面插入0\n",
    "        suffix = torch.tensor([9])  # 后面追加9\n",
    "\n",
    "        self.data = [torch.randint(1,9,size=(5,)) for _ in range(100)]\n",
    "        # self.labels = [torch.randint(0,10,size=(5,)) for _ in range(10)]\n",
    "        self.labels = [torch.cat([prefix,_,suffix],dim=0) for _ in copy.deepcopy(self.data)]\n",
    "\n",
    "    def __len__(self):\n",
    "        return len(self.data)\n",
    "\n",
    "    def __getitem__(self, idx):\n",
    "        return self.data[idx]\n",
    "\n",
    "def create_data():\n",
    "    dataset = SimpleDataset()\n",
    "    data_loader = DataLoader(dataset, batch_size=5, shuffle=True,collate_fn=collate_fn)\n",
    "    # for _ in data_loader:\n",
    "    #     print(_[0])\n",
    "    #     print(_[1])\n",
    "    #     print('\\n')\n",
    "    return data_loader\n",
    "\n",
    "for a in create_data():\n",
    "    print(a)\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "01608d7d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 1., 1., 1., 1., 1., 1.],\n",
       "        [0., 1., 1., 1., 1., 1., 1.],\n",
       "        [0., 0., 1., 1., 1., 1., 1.],\n",
       "        [0., 0., 0., 1., 1., 1., 1.],\n",
       "        [0., 0., 0., 0., 1., 1., 1.]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.triu(torch.ones(5, 7), diagonal=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "be6a37a4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T06:34:30.542605100Z",
     "start_time": "2025-09-10T06:34:30.521602700Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0., 1., 1., 1., 1., 1., 1.],\n",
       "        [0., 0., 1., 1., 1., 1., 1.],\n",
       "        [0., 0., 0., 1., 1., 1., 1.],\n",
       "        [0., 0., 0., 0., 1., 1., 1.],\n",
       "        [0., 0., 0., 0., 0., 1., 1.],\n",
       "        [0., 0., 0., 0., 0., 0., 1.],\n",
       "        [0., 0., 0., 0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.triu(torch.ones(7, 7), diagonal=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68de6690",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e03d5e3f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T06:34:32.469259Z",
     "start_time": "2025-09-10T06:34:32.466255900Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[True]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.triu(torch.ones(1, 1), diagonal=1)==0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6fa778f5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "be4c3514",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T06:36:50.205501200Z",
     "start_time": "2025-09-10T06:36:50.120502900Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0., 1., 1., 1., 1.],\n",
       "        [0., 0., 1., 1., 1.],\n",
       "        [0., 0., 0., 1., 1.],\n",
       "        [0., 0., 0., 0., 1.],\n",
       "        [0., 0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.triu(torch.ones(5, 5), diagonal=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb2a3134",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "453ca956",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(2.6369)"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a= torch.randn(2,10)\n",
    "b = torch.randint(1,10,(2,))\n",
    "\n",
    "# display(a.size(),b.size())\n",
    "# display(a.shape,b.shape)\n",
    "\n",
    "a_1 = F.log_softmax(a,dim=-1)\n",
    "loss = torch.nn.NLLLoss()\n",
    "loss(a_1,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5ad4c3a1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T06:56:02.762449Z",
     "start_time": "2025-09-10T06:56:02.681941700Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0., 1., 1., 1., 1., 1.],\n",
       "        [0., 0., 1., 1., 1., 1.],\n",
       "        [0., 0., 0., 1., 1., 1.],\n",
       "        [0., 0., 0., 0., 1., 1.],\n",
       "        [0., 0., 0., 0., 0., 1.]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.triu(torch.ones(5, 6), diagonal=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8136ad2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "6187e2a7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([2, 10, 100])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_1.size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "a77c5573",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(4.7964)"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a= torch.randn(2,10,100)\n",
    "b = torch.randint(1,100,(2,10)) # 2,10\n",
    "a_1 = F.log_softmax(a,dim=-1) # 2,10,100  \n",
    "a_1 = a_1.permute(0, 2, 1)  # 调整维度为(2,100,10)，即(batch, C, seq_len)\n",
    "loss = torch.nn.NLLLoss()\n",
    "loss(a_1,b) \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "a3408ae6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[33, 38, 35,  6, 79, 15, 17, 18, 30, 40, 67, 13, 25, 80, 82, 57, 62, 24,\n",
       "         69, 25, 96, 19, 92, 24, 88, 43, 44, 97, 90, 34, 24, 13, 20, 38, 97, 21,\n",
       "         55, 68, 51,  6, 94, 90, 67,  1, 98, 68, 64, 65, 92, 15, 34, 38, 33,  4,\n",
       "         85, 79, 96, 99, 15, 90, 38, 40, 63, 24, 21,  5,  5, 83, 93, 20, 41, 61,\n",
       "         23, 43, 14, 54, 72, 46,  8, 27, 56, 17, 34, 79, 14, 20, 66, 27, 15, 26,\n",
       "         86,  5, 74, 88, 58, 21, 36, 71, 31, 15],\n",
       "        [53, 59, 16, 61, 36, 82, 49, 95, 99, 50, 55, 84, 10,  2, 63, 22, 84, 97,\n",
       "          9, 65,  2, 70, 94, 11, 94, 84, 46, 80, 19,  2,  9, 32, 89, 53, 10, 97,\n",
       "         96, 12, 20, 83, 39, 50, 83, 41, 66, 99, 94, 72, 36, 52,  8, 60, 70, 94,\n",
       "         75, 20,  7, 85, 98, 96, 56,  4, 88, 82, 52, 34, 21, 14, 77, 53, 72, 82,\n",
       "         15, 25, 59, 32, 58, 88,  5, 87, 48, 45, 10, 51, 62, 22, 43, 66,  2, 15,\n",
       "         58, 66, 67, 50, 56, 80, 96, 71, 70,  1]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "863d99bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[-4.2776, -2.2733, -5.7627,  ..., -3.9196, -5.6802, -4.5581],\n",
       "         [-5.5972, -4.6129, -4.3914,  ..., -3.2598, -3.8729, -4.1814],\n",
       "         [-4.7601, -5.9936, -4.7435,  ..., -6.5362, -3.8805, -4.5126],\n",
       "         ...,\n",
       "         [-4.8156, -3.3593, -6.7534,  ..., -5.7833, -4.0843, -4.5369],\n",
       "         [-5.1831, -4.9121, -6.3072,  ..., -6.4986, -5.2014, -4.3037],\n",
       "         [-4.6851, -4.1770, -5.0540,  ..., -5.9683, -5.2280, -6.2134]],\n",
       "\n",
       "        [[-4.8004, -5.6264, -4.1012,  ..., -4.4429, -3.8446, -5.2025],\n",
       "         [-4.1230, -5.3999, -3.6553,  ..., -6.4763, -5.3168, -4.4091],\n",
       "         [-6.6055, -5.5476, -4.3207,  ..., -5.2975, -5.4493, -4.7458],\n",
       "         ...,\n",
       "         [-6.5075, -5.7350, -5.1993,  ..., -3.8645, -3.1200, -5.9338],\n",
       "         [-4.9526, -6.8171, -4.4862,  ..., -6.7260, -2.7436, -6.8008],\n",
       "         [-5.0007, -4.3137, -4.5176,  ..., -4.8935, -3.8654, -6.1977]]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "torch.Size([2, 10, 100])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a_1 = F.log_softmax(a,dim=-1)\n",
    "display(a_1,a_1.size())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "e27f84e2",
   "metadata": {},
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "Expected target size [2, 100], got [2, 10]",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[48], line 3\u001b[0m\n\u001b[0;32m      1\u001b[0m loss \u001b[38;5;241m=\u001b[39m torch\u001b[38;5;241m.\u001b[39mnn\u001b[38;5;241m.\u001b[39mNLLLoss()\n\u001b[1;32m----> 3\u001b[0m \u001b[43mloss\u001b[49m\u001b[43m(\u001b[49m\u001b[43mF\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlog_softmax\u001b[49m\u001b[43m(\u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43mdim\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43mb\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\python310\\lib\\site-packages\\torch\\nn\\modules\\module.py:1751\u001b[0m, in \u001b[0;36mModule._wrapped_call_impl\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m   1749\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_compiled_call_impl(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)  \u001b[38;5;66;03m# type: ignore[misc]\u001b[39;00m\n\u001b[0;32m   1750\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m-> 1751\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_call_impl(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
      "File \u001b[1;32md:\\python310\\lib\\site-packages\\torch\\nn\\modules\\module.py:1762\u001b[0m, in \u001b[0;36mModule._call_impl\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m   1757\u001b[0m \u001b[38;5;66;03m# If we don't have any hooks, we want to skip the rest of the logic in\u001b[39;00m\n\u001b[0;32m   1758\u001b[0m \u001b[38;5;66;03m# this function, and just call forward.\u001b[39;00m\n\u001b[0;32m   1759\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m (\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_backward_hooks \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_backward_pre_hooks \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_forward_hooks \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_forward_pre_hooks\n\u001b[0;32m   1760\u001b[0m         \u001b[38;5;129;01mor\u001b[39;00m _global_backward_pre_hooks \u001b[38;5;129;01mor\u001b[39;00m _global_backward_hooks\n\u001b[0;32m   1761\u001b[0m         \u001b[38;5;129;01mor\u001b[39;00m _global_forward_hooks \u001b[38;5;129;01mor\u001b[39;00m _global_forward_pre_hooks):\n\u001b[1;32m-> 1762\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m forward_call(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[0;32m   1764\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m   1765\u001b[0m called_always_called_hooks \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mset\u001b[39m()\n",
      "File \u001b[1;32md:\\python310\\lib\\site-packages\\torch\\nn\\modules\\loss.py:251\u001b[0m, in \u001b[0;36mNLLLoss.forward\u001b[1;34m(self, input, target)\u001b[0m\n\u001b[0;32m    250\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21mforward\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;28minput\u001b[39m: Tensor, target: Tensor) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Tensor:\n\u001b[1;32m--> 251\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mF\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mnll_loss\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    252\u001b[0m \u001b[43m        \u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m    253\u001b[0m \u001b[43m        \u001b[49m\u001b[43mtarget\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    254\u001b[0m \u001b[43m        \u001b[49m\u001b[43mweight\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mweight\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    255\u001b[0m \u001b[43m        \u001b[49m\u001b[43mignore_index\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mignore_index\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    256\u001b[0m \u001b[43m        \u001b[49m\u001b[43mreduction\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreduction\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    257\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\python310\\lib\\site-packages\\torch\\nn\\functional.py:3158\u001b[0m, in \u001b[0;36mnll_loss\u001b[1;34m(input, target, weight, size_average, ignore_index, reduce, reduction)\u001b[0m\n\u001b[0;32m   3156\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m size_average \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mor\u001b[39;00m reduce \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m   3157\u001b[0m     reduction \u001b[38;5;241m=\u001b[39m _Reduction\u001b[38;5;241m.\u001b[39mlegacy_get_string(size_average, reduce)\n\u001b[1;32m-> 3158\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_C\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_nn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mnll_loss_nd\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m   3159\u001b[0m \u001b[43m    \u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtarget\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mweight\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m_Reduction\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_enum\u001b[49m\u001b[43m(\u001b[49m\u001b[43mreduction\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mignore_index\u001b[49m\n\u001b[0;32m   3160\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mRuntimeError\u001b[0m: Expected target size [2, 100], got [2, 10]"
     ]
    }
   ],
   "source": [
    "loss = torch.nn.NLLLoss()\n",
    "\n",
    "loss(F.log_softmax(a,dim=-1),b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96290a5e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d44e0205",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "29a69c53",
   "metadata": {},
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "\"softmax_lastdim_kernel_impl\" not implemented for 'Long'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[34], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mF\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msoftmax\u001b[49m\u001b[43m(\u001b[49m\u001b[43mb\u001b[49m\u001b[43m,\u001b[49m\u001b[43mdim\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\python310\\lib\\site-packages\\torch\\nn\\functional.py:2140\u001b[0m, in \u001b[0;36msoftmax\u001b[1;34m(input, dim, _stacklevel, dtype)\u001b[0m\n\u001b[0;32m   2138\u001b[0m     dim \u001b[38;5;241m=\u001b[39m _get_softmax_dim(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msoftmax\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28minput\u001b[39m\u001b[38;5;241m.\u001b[39mdim(), _stacklevel)\n\u001b[0;32m   2139\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m dtype \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m-> 2140\u001b[0m     ret \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43minput\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msoftmax\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdim\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m   2141\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m   2142\u001b[0m     ret \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m\u001b[38;5;241m.\u001b[39msoftmax(dim, dtype\u001b[38;5;241m=\u001b[39mdtype)\n",
      "\u001b[1;31mRuntimeError\u001b[0m: \"softmax_lastdim_kernel_impl\" not implemented for 'Long'"
     ]
    }
   ],
   "source": [
    "F.softmax(b,dim=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "71f81f6a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[-5.4027, -3.0738, -4.0114,  ..., -5.9493, -4.6393, -6.0648],\n",
       "         [-5.3978, -3.8114, -4.7052,  ..., -4.9718, -6.3198, -5.7116],\n",
       "         [-5.6478, -4.2701, -3.2718,  ..., -3.9486, -4.0628, -3.9771],\n",
       "         ...,\n",
       "         [-5.0803, -4.6956, -6.2388,  ..., -4.3755, -6.5728, -4.2485],\n",
       "         [-3.7328, -5.3953, -5.2323,  ..., -5.6315, -5.0435, -5.8314],\n",
       "         [-6.7192, -5.3184, -6.2996,  ..., -3.2657, -4.4302, -4.3847]],\n",
       "\n",
       "        [[-6.7751, -3.7463, -7.5925,  ..., -5.7477, -5.9251, -4.2158],\n",
       "         [-5.6115, -4.6816, -6.8891,  ..., -5.2322, -5.1570, -3.3729],\n",
       "         [-2.9671, -4.4348, -5.8228,  ..., -4.8498, -4.0887, -4.3499],\n",
       "         ...,\n",
       "         [-3.6568, -4.0066, -5.3818,  ..., -4.0888, -4.4618, -4.7906],\n",
       "         [-4.7844, -3.4167, -5.0504,  ..., -5.7129, -3.3459, -4.1508],\n",
       "         [-4.1637, -4.4088, -3.0273,  ..., -4.4028, -4.7514, -5.5155]]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "F.log_softmax(a,dim=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cdae76bf",
   "metadata": {},
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "Expected target size [2, 100], got [2, 10]",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[32], line 3\u001b[0m\n\u001b[0;32m      1\u001b[0m loss \u001b[38;5;241m=\u001b[39m torch\u001b[38;5;241m.\u001b[39mnn\u001b[38;5;241m.\u001b[39mNLLLoss()\n\u001b[1;32m----> 3\u001b[0m \u001b[43mloss\u001b[49m\u001b[43m(\u001b[49m\u001b[43mF\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlog_softmax\u001b[49m\u001b[43m(\u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43mdim\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43mb\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\python310\\lib\\site-packages\\torch\\nn\\modules\\module.py:1751\u001b[0m, in \u001b[0;36mModule._wrapped_call_impl\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m   1749\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_compiled_call_impl(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)  \u001b[38;5;66;03m# type: ignore[misc]\u001b[39;00m\n\u001b[0;32m   1750\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m-> 1751\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_call_impl(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
      "File \u001b[1;32md:\\python310\\lib\\site-packages\\torch\\nn\\modules\\module.py:1762\u001b[0m, in \u001b[0;36mModule._call_impl\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m   1757\u001b[0m \u001b[38;5;66;03m# If we don't have any hooks, we want to skip the rest of the logic in\u001b[39;00m\n\u001b[0;32m   1758\u001b[0m \u001b[38;5;66;03m# this function, and just call forward.\u001b[39;00m\n\u001b[0;32m   1759\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m (\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_backward_hooks \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_backward_pre_hooks \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_forward_hooks \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_forward_pre_hooks\n\u001b[0;32m   1760\u001b[0m         \u001b[38;5;129;01mor\u001b[39;00m _global_backward_pre_hooks \u001b[38;5;129;01mor\u001b[39;00m _global_backward_hooks\n\u001b[0;32m   1761\u001b[0m         \u001b[38;5;129;01mor\u001b[39;00m _global_forward_hooks \u001b[38;5;129;01mor\u001b[39;00m _global_forward_pre_hooks):\n\u001b[1;32m-> 1762\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m forward_call(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[0;32m   1764\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m   1765\u001b[0m called_always_called_hooks \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mset\u001b[39m()\n",
      "File \u001b[1;32md:\\python310\\lib\\site-packages\\torch\\nn\\modules\\loss.py:251\u001b[0m, in \u001b[0;36mNLLLoss.forward\u001b[1;34m(self, input, target)\u001b[0m\n\u001b[0;32m    250\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21mforward\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;28minput\u001b[39m: Tensor, target: Tensor) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Tensor:\n\u001b[1;32m--> 251\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mF\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mnll_loss\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m    252\u001b[0m \u001b[43m        \u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[0;32m    253\u001b[0m \u001b[43m        \u001b[49m\u001b[43mtarget\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    254\u001b[0m \u001b[43m        \u001b[49m\u001b[43mweight\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mweight\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    255\u001b[0m \u001b[43m        \u001b[49m\u001b[43mignore_index\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mignore_index\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    256\u001b[0m \u001b[43m        \u001b[49m\u001b[43mreduction\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreduction\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    257\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\python310\\lib\\site-packages\\torch\\nn\\functional.py:3158\u001b[0m, in \u001b[0;36mnll_loss\u001b[1;34m(input, target, weight, size_average, ignore_index, reduce, reduction)\u001b[0m\n\u001b[0;32m   3156\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m size_average \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mor\u001b[39;00m reduce \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m   3157\u001b[0m     reduction \u001b[38;5;241m=\u001b[39m _Reduction\u001b[38;5;241m.\u001b[39mlegacy_get_string(size_average, reduce)\n\u001b[1;32m-> 3158\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_C\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_nn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mnll_loss_nd\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m   3159\u001b[0m \u001b[43m    \u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtarget\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mweight\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m_Reduction\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_enum\u001b[49m\u001b[43m(\u001b[49m\u001b[43mreduction\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mignore_index\u001b[49m\n\u001b[0;32m   3160\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mRuntimeError\u001b[0m: Expected target size [2, 100], got [2, 10]"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b9a3169a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100000.0"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1e5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e335c4ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ True, False, False, False, False, False, False, False, False, False],\n",
       "        [ True,  True, False, False, False, False, False, False, False, False],\n",
       "        [ True,  True,  True, False, False, False, False, False, False, False],\n",
       "        [ True,  True,  True,  True, False, False, False, False, False, False],\n",
       "        [ True,  True,  True,  True,  True, False, False, False, False, False]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.triu(torch.ones(5, 10), diagonal=1)==0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "34abbbbb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
       "        [0., 0., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
       "        [0., 0., 0., 1., 1., 1., 1., 1., 1., 1.],\n",
       "        [0., 0., 0., 0., 1., 1., 1., 1., 1., 1.],\n",
       "        [0., 0., 0., 0., 0., 1., 1., 1., 1., 1.],\n",
       "        [0., 0., 0., 0., 0., 0., 1., 1., 1., 1.],\n",
       "        [0., 0., 0., 0., 0., 0., 0., 1., 1., 1.],\n",
       "        [0., 0., 0., 0., 0., 0., 0., 0., 1., 1.],\n",
       "        [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],\n",
       "        [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.triu(torch.ones(10, 10), diagonal=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "28aafd26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Tensor"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type((torch.triu(torch.ones(10, 10), diagonal=1)==0).bool())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4c2b41fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Tensor"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(torch.triu(torch.ones(10, 10), diagonal=1)==0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "354e7e45",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q 5 10 512  5 10 \n",
    "\n",
    "k 5 20 512  5 20\n",
    "\n",
    "\n",
    "5 10 20 \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "initial_id",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-31T08:19:09.254201Z",
     "start_time": "2025-08-31T08:19:09.244340Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "73090a1454eab2e5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-31T08:30:15.452586Z",
     "start_time": "2025-08-31T08:30:15.440168Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10**2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "869b5a13b6ca151",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-31T08:30:17.848041Z",
     "start_time": "2025-08-31T08:30:17.840706Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10**3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f71b630d1229ff95",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-31T08:36:50.896797Z",
     "start_time": "2025-08-31T08:36:50.255290Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d3becc60bafbd82",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-31T08:37:49.403220Z",
     "start_time": "2025-08-31T08:37:49.387307Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0],\n",
       "       [1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4],\n",
       "       [5],\n",
       "       [6],\n",
       "       [7],\n",
       "       [8],\n",
       "       [9]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=np.arange(0,10).reshape(10,-1)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b70d82bcead6506b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-31T08:38:08.061119Z",
     "start_time": "2025-08-31T08:38:08.044261Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b=np.arange(1,5)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "99abc2d4095fa393",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-31T08:39:46.734374Z",
     "start_time": "2025-08-31T08:39:46.727792Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.        , 0.        , 0.        , 0.        ],\n",
       "       [0.63095734, 0.39810717, 0.25118864, 0.15848932],\n",
       "       [1.26191469, 0.79621434, 0.50237729, 0.31697864],\n",
       "       [1.89287203, 1.19432151, 0.75356593, 0.47546796],\n",
       "       [2.52382938, 1.59242868, 1.00475457, 0.63395728],\n",
       "       [3.15478672, 1.99053585, 1.25594322, 0.7924466 ],\n",
       "       [3.78574407, 2.38864302, 1.50713186, 0.95093592],\n",
       "       [4.41670141, 2.78675019, 1.7583205 , 1.10942523],\n",
       "       [5.04765876, 3.18485736, 2.00950915, 1.26791455],\n",
       "       [5.6786161 , 3.58296453, 2.26069779, 1.42640387]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a/(1000**(2*b/30))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4e815c48ebcb400c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-31T10:46:52.113101Z",
     "start_time": "2025-08-31T10:46:52.101349Z"
    }
   },
   "outputs": [],
   "source": [
    "a=np.random.randint(0,10,size=(10,10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "17f79c7741e9bcc",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-31T10:46:55.655751Z",
     "start_time": "2025-08-31T10:46:55.638648Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 5, 7, 5, 9, 2, 3, 7, 4, 6],\n",
       "       [6, 1, 2, 9, 6, 2, 5, 9, 8, 0],\n",
       "       [8, 9, 9, 1, 9, 7, 3, 6, 5, 7],\n",
       "       [9, 4, 9, 5, 8, 4, 4, 8, 5, 4],\n",
       "       [0, 6, 8, 1, 6, 3, 8, 8, 6, 7],\n",
       "       [1, 4, 5, 6, 8, 0, 1, 5, 4, 8],\n",
       "       [2, 5, 3, 7, 1, 4, 0, 6, 0, 8],\n",
       "       [0, 9, 2, 5, 6, 3, 1, 2, 7, 7],\n",
       "       [0, 9, 8, 0, 5, 6, 2, 5, 9, 0],\n",
       "       [4, 8, 3, 4, 2, 2, 5, 7, 0, 0]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c04f4a697ad9ab8a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-31T10:47:27.859788Z",
     "start_time": "2025-08-31T10:47:27.844011Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 5, 3, 6],\n",
       "       [6, 9, 5, 0],\n",
       "       [8, 1, 3, 7],\n",
       "       [9, 5, 4, 4],\n",
       "       [0, 1, 8, 7],\n",
       "       [1, 6, 1, 8],\n",
       "       [2, 7, 0, 8],\n",
       "       [0, 5, 1, 7],\n",
       "       [0, 0, 2, 0],\n",
       "       [4, 4, 5, 0]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:,::3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6246ea51465a76cd",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-31T16:08:04.762228Z",
     "start_time": "2025-08-31T16:08:00.811817Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "21b819b631c2c4ba",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-31T13:31:12.159602Z",
     "start_time": "2025-08-31T13:31:12.146380Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(10.)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.sqrt(torch.tensor(100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "969c2f7ac4aa050b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-31T13:33:01.446345Z",
     "start_time": "2025-08-31T13:33:01.438865Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.6501,  0.5448,  0.3409,  0.2621],\n",
       "        [ 0.5068,  1.3433, -0.8285, -0.9790],\n",
       "        [-0.2847,  1.7067, -0.1983,  0.1472],\n",
       "        [ 0.4176,  0.7763,  0.5447,  0.8418],\n",
       "        [ 0.4712, -0.0043,  0.4105, -0.8373],\n",
       "        [-0.1956, -1.0857,  1.7688,  0.7794],\n",
       "        [ 0.7046, -0.3028,  2.4334, -1.0141],\n",
       "        [-0.1352, -0.6535, -0.4068,  0.3623],\n",
       "        [-1.8546,  1.2492, -1.2539,  1.5344],\n",
       "        [ 0.6656,  0.4984,  0.4133, -0.0262]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.randn(10,4)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "922f5c715710c710",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-31T13:33:05.526672Z",
     "start_time": "2025-08-31T13:33:05.509865Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.3019, 0.2717, 0.2216, 0.2048],\n",
       "        [0.2633, 0.6078, 0.0693, 0.0596],\n",
       "        [0.0913, 0.6686, 0.0995, 0.1406],\n",
       "        [0.1963, 0.2809, 0.2229, 0.2999],\n",
       "        [0.3530, 0.2194, 0.3322, 0.0954],\n",
       "        [0.0893, 0.0367, 0.6371, 0.2369],\n",
       "        [0.1393, 0.0509, 0.7848, 0.0250],\n",
       "        [0.2499, 0.1488, 0.1904, 0.4109],\n",
       "        [0.0183, 0.4070, 0.0333, 0.5414],\n",
       "        [0.3201, 0.2708, 0.2487, 0.1603]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.nn.functional.softmax(a,dim=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "9306c0ad9a1dae0f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-31T13:33:15.950690Z",
     "start_time": "2025-08-31T13:33:15.932836Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000,\n",
       "        1.0000])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.nn.functional.softmax(a,dim=-1).sum(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "f9dffc1f868cfa32",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-31T13:34:38.313550Z",
     "start_time": "2025-08-31T13:34:38.305547Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1000000000.0"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "-1e9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "bd421751e4add403",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-31T13:35:31.686115Z",
     "start_time": "2025-08-31T13:35:31.677218Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.6501,  0.5448,  0.3409,  0.2621],\n",
       "        [ 0.5068,  1.3433, -0.8285, -0.9790],\n",
       "        [-0.2847,  1.7067, -0.1983,  0.1472],\n",
       "        [ 0.4176,  0.7763,  0.5447,  0.8418],\n",
       "        [ 0.4712, -0.0043,  0.4105, -0.8373],\n",
       "        [-0.1956, -1.0857,  1.7688,  0.7794],\n",
       "        [ 0.7046, -0.3028,  2.4334, -1.0141],\n",
       "        [-0.1352, -0.6535, -0.4068,  0.3623],\n",
       "        [-1.8546,  1.2492, -1.2539,  1.5344],\n",
       "        [ 0.6656,  0.4984,  0.4133, -0.0262]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2f24fde23f2e81f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-31T16:09:18.528925Z",
     "start_time": "2025-08-31T16:09:18.515084Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.5549, 0.7527, 0.3605],\n",
       "        [0.0525, 0.9205, 0.1652],\n",
       "        [0.0842, 0.1737, 0.4874],\n",
       "        [0.0615, 0.3403, 0.8833],\n",
       "        [0.8156, 0.6983, 0.6021],\n",
       "        [0.9017, 0.9539, 0.4105],\n",
       "        [0.6120, 0.1016, 0.8438],\n",
       "        [0.8845, 0.4848, 0.1994],\n",
       "        [0.8371, 0.8569, 0.8297],\n",
       "        [0.1591, 0.9919, 0.2419]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "tensor([[1., 1., 1.],\n",
       "        [0., 1., 1.],\n",
       "        [0., 0., 1.],\n",
       "        [0., 0., 0.],\n",
       "        [0., 0., 0.],\n",
       "        [0., 0., 0.],\n",
       "        [0., 0., 0.],\n",
       "        [0., 0., 0.],\n",
       "        [0., 0., 0.],\n",
       "        [0., 0., 0.]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a=torch.rand(10,3)\n",
    "b = torch.triu(torch.ones(10,3),diagonal=0)\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4c9036a470903486",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-31T16:09:27.905939Z",
     "start_time": "2025-08-31T16:09:27.892275Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1.0000e+09, -1.0000e+09, -1.0000e+09],\n",
       "        [ 5.2532e-02, -1.0000e+09, -1.0000e+09],\n",
       "        [ 8.4201e-02,  1.7374e-01, -1.0000e+09],\n",
       "        [ 6.1469e-02,  3.4027e-01,  8.8333e-01],\n",
       "        [ 8.1556e-01,  6.9827e-01,  6.0213e-01],\n",
       "        [ 9.0174e-01,  9.5392e-01,  4.1048e-01],\n",
       "        [ 6.1202e-01,  1.0162e-01,  8.4380e-01],\n",
       "        [ 8.8450e-01,  4.8478e-01,  1.9938e-01],\n",
       "        [ 8.3710e-01,  8.5695e-01,  8.2970e-01],\n",
       "        [ 1.5907e-01,  9.9193e-01,  2.4189e-01]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.masked_fill(b==1,-1e9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "71642d036a829a41",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-31T13:38:29.050251Z",
     "start_time": "2025-08-31T13:38:28.785811Z"
    }
   },
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "The size of tensor a (3) must match the size of tensor b (4) at non-singleton dimension 1",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[44], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43ma\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmasked_fill\u001b[49m\u001b[43m(\u001b[49m\u001b[43mb\u001b[49m\u001b[43m,\u001b[49m\u001b[38;5;28;43mfloat\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m-inf\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mRuntimeError\u001b[0m: The size of tensor a (3) must match the size of tensor b (4) at non-singleton dimension 1"
     ]
    }
   ],
   "source": [
    "a.masked_fill(b,float('-inf'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9fc8c71fc6ddccdf",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e22a70b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.1191, 1.4947, 0.5050],\n",
       "        [0.4368, 0.1512, 1.4614]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.randn(2,3)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b43b1473",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.1191, 0.4368],\n",
       "        [1.4947, 0.1512],\n",
       "        [0.5050, 1.4614]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.transpose(1,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "51e81490",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 8.0092, -0.6197],\n",
       "        [-0.6197,  1.0023]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a@a.transpose(1,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4b85d188",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[-1.4760, -0.4135,  0.3156],\n",
       "         [ 0.7320, -1.2627, -0.6187]],\n",
       "\n",
       "        [[ 1.2345, -0.2102, -1.1709],\n",
       "         [ 0.1568, -0.7635, -0.2343]],\n",
       "\n",
       "        [[ 0.0669, -0.7005, -1.1356],\n",
       "         [ 1.4469,  0.7387,  0.5415]],\n",
       "\n",
       "        [[-0.5665,  1.2695, -0.7324],\n",
       "         [-0.7052, -0.3449, -1.0426]],\n",
       "\n",
       "        [[-0.9599,  0.6129,  0.0442],\n",
       "         [-0.7725,  0.1197, -0.0255]]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=torch.randn(5,2,3)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "fba9dea1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[-1.4760,  0.7320],\n",
       "         [-0.4135, -1.2627],\n",
       "         [ 0.3156, -0.6187]],\n",
       "\n",
       "        [[ 1.2345,  0.1568],\n",
       "         [-0.2102, -0.7635],\n",
       "         [-1.1709, -0.2343]],\n",
       "\n",
       "        [[ 0.0669,  1.4469],\n",
       "         [-0.7005,  0.7387],\n",
       "         [-1.1356,  0.5415]],\n",
       "\n",
       "        [[-0.5665, -0.7052],\n",
       "         [ 1.2695, -0.3449],\n",
       "         [-0.7324, -1.0426]],\n",
       "\n",
       "        [[-0.9599, -0.7725],\n",
       "         [ 0.6129,  0.1197],\n",
       "         [ 0.0442, -0.0255]]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.transpose(2,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d0869c02",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 2.4493, -0.7536],\n",
       "         [-0.7536,  2.5129]],\n",
       "\n",
       "        [[ 2.9392,  0.6284],\n",
       "         [ 0.6284,  0.6623]],\n",
       "\n",
       "        [[ 1.7848, -1.0356],\n",
       "         [-1.0356,  2.9325]],\n",
       "\n",
       "        [[ 2.4689,  0.7252],\n",
       "         [ 0.7252,  1.7033]],\n",
       "\n",
       "        [[ 1.2991,  0.8138],\n",
       "         [ 0.8138,  0.6117]]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b=torch.matmul(a,a.transpose(2,1))\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6e21ba7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ True, False],\n",
       "        [ True,  True],\n",
       "        [False, False],\n",
       "        [ True, False],\n",
       "        [ True, False]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mask = torch.randn(5,2)>0\n",
    "mask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "1e1b8218",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ True, False],\n",
       "        [ True,  True],\n",
       "        [False, False],\n",
       "        [ True, False],\n",
       "        [ True, False]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "b2393b7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ True, False]],\n",
       "\n",
       "        [[ True,  True]],\n",
       "\n",
       "        [[False, False]],\n",
       "\n",
       "        [[ True, False]],\n",
       "\n",
       "        [[ True, False]]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mask_view = mask.view(5,1,2)\n",
    "mask_view"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b08f2cf4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 2.4493, -0.7536],\n",
       "         [-0.7536,  2.5129]],\n",
       "\n",
       "        [[ 2.9392,  0.6284],\n",
       "         [ 0.6284,  0.6623]],\n",
       "\n",
       "        [[ 1.7848, -1.0356],\n",
       "         [-1.0356,  2.9325]],\n",
       "\n",
       "        [[ 2.4689,  0.7252],\n",
       "         [ 0.7252,  1.7033]],\n",
       "\n",
       "        [[ 1.2991,  0.8138],\n",
       "         [ 0.8138,  0.6117]]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "29869910",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 2.4493e+00, -1.0000e+09],\n",
       "         [-7.5361e-01, -1.0000e+09]],\n",
       "\n",
       "        [[ 2.9392e+00,  6.2839e-01],\n",
       "         [ 6.2839e-01,  6.6235e-01]],\n",
       "\n",
       "        [[-1.0000e+09, -1.0000e+09],\n",
       "         [-1.0000e+09, -1.0000e+09]],\n",
       "\n",
       "        [[ 2.4689e+00, -1.0000e+09],\n",
       "         [ 7.2520e-01, -1.0000e+09]],\n",
       "\n",
       "        [[ 1.2991e+00, -1.0000e+09],\n",
       "         [ 8.1375e-01, -1.0000e+09]]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.masked_fill(mask_view==False, -1e9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "10458078",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[1.0000, 0.0000],\n",
       "         [1.0000, 0.0000]],\n",
       "\n",
       "        [[0.9098, 0.0902],\n",
       "         [0.4915, 0.5085]],\n",
       "\n",
       "        [[0.5000, 0.5000],\n",
       "         [0.5000, 0.5000]],\n",
       "\n",
       "        [[1.0000, 0.0000],\n",
       "         [1.0000, 0.0000]],\n",
       "\n",
       "        [[1.0000, 0.0000],\n",
       "         [1.0000, 0.0000]]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.nn.functional.softmax(b.masked_fill(mask_view==False, -1e9),dim=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cbfec6aa",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
